"""
首页函数
"""
import haomai.tmpTest
from django.conf import settings
# Create your views here.
from django.core.cache import cache
from django.forms.models import model_to_dict
from django.http import HttpResponse
from django.shortcuts import render

from haomai.constant import appkey, responseJson
from home.models import Doctor


def index(request):
    # return HttpResponse("Hello, world. You're at the polls index.")
    return render(request, 'home/index.html')


def test(request):
    a = 10
    b = 20
    c = a + b
    # conn = get_redis_connection('default')
    # conn.set('name',100)
    cache.set('key', 11, 10)
    # print(str(cache.get('key'),'utf-8'))
    print(cache.get('key'))
    return HttpResponse(c)


def test1(request):
    """添加测试"""
    rst = cache.get('key')
    if rst:
        print(rst)
    return HttpResponse(rst)


def chat(request):
    """测试聊天首页面"""
    # return HttpResponse("Hello, world. You're at the polls index.")4
    return render(request, 'chat.html')


# 查询数据
def getlist():
    """获取医生列表"""
    lists = Doctor.objects.all()
    print(lists)


def updatedoctor(request):
    """更新数据"""
    # 插入
    # a = Doctor(name="张的",sex = "1",update_time = timezone.now())
    # a.save()
    # print(a.id)
    # 查询
    # lists = Doctor.objects.all()
    # for i in lists:
    #     print(i.name)

    try:
        stu = Doctor.objects.get(name="张帅")
        print(stu.update_time)
    except Doctor.DoesNotExist:
        print("不存在")
    return HttpResponse()


def testjson(request):
    """
    测试json数据
    """
    try:
        stus = Doctor.objects.get(name="张帅").to_dict()
        print(stus)
    except Doctor.DoesNotExist:
        print("不存在")
    else:
        print("其他异常")
    print(appkey)
    objc = Doctor.objects.get(pk=1)
    key = settings.CONSTANTS['key']
    print(model_to_dict(objc))
    return responseJson(200, model_to_dict(objc))


def testpy():
    print("测试脚本")


if __name__ == '__main__':
    a = testjson("")
    print(a)
